//
//  NWalletTableViewController.m
//  News
//
//  Created by zhangtao on 2018/10/7.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NWalletTableViewController.h"
#import "NWalletHeaderView.h"
#import "NWalletCell.h"
#import "NWalletDetailViewController.h"
#import "NWalletGBWithdrawViewController.h"
#import "NWalletXJWithdrawViewController.h"
#import "SubscriberWalletSummaryModel.h"
#import "TotalCoinOfRewardedModel.h"

@interface NWalletTableViewController ()

@property (nonatomic, strong) NWalletHeaderView *head;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) TotalCoinOfRewardedModel *totalCoinOfRewardedModel;
@property (nonatomic, strong) SubscriberWalletSummaryModel *subscriberWalletSummaryModel;

@end

@implementation NWalletTableViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self getSubscriberWalletSummary];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self setHidesBottomBarWhenPushed:NO];
}

-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"NWalletHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NWalletCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)getSubscriberWalletSummary
{
    [[NHTTPManage shareInstance]afGet:HTTP_GET_GetSubscriberWalletSummary andParam:@{@"cuid":CUID,@"token":TOKEN} success:^(id  _Nonnull responseObject) {
        self.subscriberWalletSummaryModel = [SubscriberWalletSummaryModel modelObjectWithDictionary:responseObject[@"Data"]];
        [self.head bindData:self.subscriberWalletSummaryModel];
    } failture:^(id  _Nonnull error) {
        NSLog(@"%@",error);
    }];
    [[NHTTPManage shareInstance]afGet:HTTP_GET_GetTotalCoinOfRewarded andParam:@{@"cuid":CUID,@"token":TOKEN} success:^(id  _Nonnull responseObject) {
        self.totalCoinOfRewardedModel = [TotalCoinOfRewardedModel modelObjectWithDictionary:responseObject[@"Data"]];
        [self.head bindData1:self.totalCoinOfRewardedModel];
    } failture:^(id  _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    29:16+29:16*2/5-10+50
    return ACEWidth*16/29 + ACEWidth*16/29*2/5+30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    NWalletHeaderView *head = [[NWalletHeaderView alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 60)];
    //[[NSBundle mainBundle] loadNibNamed:@"NWalletHeaderView" owner:self options:nil];
    //[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    self.head = [[NSBundle mainBundle]loadNibNamed:@"NWalletHeaderView" owner:self options:nil].firstObject;
    self.head.WalletHeadCallBack = ^(WALLET_HEAD_CLICK_TAG tag) {
        
        if (tag == walletHead_tiXian) {
            NWalletXJWithdrawViewController *xj = [[NWalletXJWithdrawViewController alloc]initWithNibName:@"NWalletXJWithdrawViewController" bundle:nil];
            xj.hidesBottomBarWhenPushed = YES;
            xj.money = @(self.totalCoinOfRewardedModel.coinAccount);
            [self.navigationController pushViewController:xj animated:YES];
            return ;
        }
        
        if(tag == walletHead_zheXian){
            NWalletGBWithdrawViewController *zx = [[NWalletGBWithdrawViewController alloc]initWithNibName:@"NWalletGBWithdrawViewController" bundle:nil];
            zx.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:zx animated:YES];
            return;
        }
        if (tag == walletHead_help) {
            NWebViewController *vc = [[NWebViewController alloc] init];
            vc.netHtml = [NSString stringWithFormat:@"%@%@",baseH5Url,H5_help];
            vc.title = @"帮助中心";
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            NWalletDetailViewController *detail = [[NWalletDetailViewController alloc]initWithNibName:@"NWalletDetailViewController" bundle:nil];
            
            if (tag == walletHead_guBiDetail || tag == walletHead_cashDetail) {
                detail.isBiList = YES;
            }else if (tag == walletHead_guBiSort){
                detail.isBiList = NO;
            }else if (tag == walletHead_cashDetail){
                
            }
            
            detail.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detail animated:YES];
        } 
    };
    return self.head;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NWalletCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSMutableDictionary *dic = self.dataArray[indexPath.row];
    NSString *imgName = dic[@"img"];
    cell.iconImgView.image = [UIImage imageNamed:imgName];
    cell.nameLab.text = dic[@"title"];
    cell.subLab.text = dic[@"delTitle"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = self.dataArray[indexPath.row];
    NWebViewController *vc = [[NWebViewController alloc] init];
    vc.netHtml = [NSString stringWithFormat:@"%@%@%ld",baseH5Url,H5_Guide,(long)indexPath.row +1];
    vc.title = dic[@"title"];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithArray:@[
                                                      [NSMutableDictionary dictionaryWithDictionary:@{@"img":@"48-48lsyd",@"title":@"每天前5篇阅读",@"delTitle":@"每天前5篇阅读，每次阅读奖励0.1~1枚股币"}],[NSMutableDictionary dictionaryWithDictionary:@{@"img":@"48-48yqxyh",@"title":@"邀请新用户加入",@"delTitle":@"邀请一位新用户，奖励10枚股币"}],[NSMutableDictionary dictionaryWithDictionary:@{@"img":@"48-48pl1",@"title":@"评论文章",@"delTitle":@"评论文章，并通过作者审核奖励0.1枚股币"}],[NSMutableDictionary dictionaryWithDictionary:@{@"img":@"48-48fx",@"title":@"分享优质内容",@"delTitle":@"发布文章，文章每获得一个阅读奖励0.5枚股币"}],[NSMutableDictionary dictionaryWithDictionary:@{@"img":@"48-48cjzt",@"title":@"创建主题",@"delTitle":@"创建主题后，主体每获得一个关注奖励0.1枚股币"}]]];
    }
    return _dataArray;
}

@end
